﻿using System;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
namespace NotebookS2
{
    public partial class Receipt_Check : Form
    {
        public Receipt_Check()
        {
            InitializeComponent();
        }

        public string ActiveUserID { get; set; }
        public string ConnectionString { get; set; }

        public void ShowSearch()
        {
            FND_CHECKRECEIPT oForm = new FND_CHECKRECEIPT();
            oForm.DocumentType = cboDocType.Text;
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            CheckReceiptManager oManager = new CheckReceiptManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dt = oManager.GetCheckEntry(oForm.SelectedDocumentNumber);

            oManager.Close();

            DataRow oRow = dt.Rows[0];

            txtDocNum.Text = oRow["DocNum"].ToString();
            txtReference.Text = oRow["Reference"].ToString();
            txtRemarks.Text = oRow["Remarks"].ToString();
            cboDocType.Text = oRow["DocType"].ToString();
            txtReceiptNumber.Text = oRow["ReceiptNumber"].ToString();
            dtPostingDate.EditValue = (DateTime)oRow["PostingDate"];
            txtTINNumber.Text = oRow["TINNumber"].ToString();

            radioSelection.SelectedIndex = Convert.ToInt16(Convert.ToBoolean(oRow["IsAccount"]));
            txtCustCode.Text = oRow["PayeeCode"].ToString();
            txtCustName.Text = oRow["PayeeName"].ToString();
            txtAddress.Text = oRow["AddressOrRemarks"].ToString();

            cboBankCodes.EditValue = oRow["BankName"].ToString();
            txtCheckNumber.Text = oRow["CheckNumber"].ToString();
            txtCheckAmount.Text = oRow["CheckAmount"].ToString();
            dtDueDate.EditValue  = Convert.ToDateTime(oRow["CheckDate"]);
            txtCheckStatus.Text = oRow["CheckStatus"].ToString();

            txtStatus.Text = oRow["DocStatus"].ToString();
            txtCreatedBy.Text = oRow["CreatedBy"].ToString();
            dtDateCreated.EditValue = (DateTime)oRow["DateCreated"];
            txtModifiedBy.Text = oRow["ModifiedBy"].ToString();
            dtDateModified.EditValue = (DateTime)oRow["DateModified"];

            btnAdd.Text = "Update";
        }

        void BindBanks()
        {
            BankManager oManager = new BankManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable tbl = oManager.GetBanks();

            oManager.Close();

            
            cboBankCodes.Properties.DataSource = tbl;
            cboBankCodes.Properties.DisplayMember = "BankName";
            cboBankCodes.Properties.ValueMember = "BankCode";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("BankName", "BankName", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboBankCodes.Properties.Columns.Add(col);
            cboBankCodes.Properties.ShowHeader = false;
            oManager.Close();

        }

        void PostDocument(string _DocNumber)
        {
            CheckReceiptManager oManager = new CheckReceiptManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.PostDocument(_DocNumber);

            oManager.Close();

            txtStatus.Text = "Posted";
        }

        void CancelDocument(string _DocNumber)
        {
            CheckReceiptManager oManager = new CheckReceiptManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CancelDocument(_DocNumber);

            oManager.Close();
            txtStatus.Text = "Canceled";
        }

        void CloseDocument(string _DocNumber)
        {
            CheckReceiptManager oManager = new CheckReceiptManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CloseDocument(_DocNumber);

            oManager.Close();

            txtStatus.Text = "Closed";
        }

        void InitializeSeries()
        {
            string ObjectType = "17";
            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();


            if (oSeries.IsAutoNumber(ObjectType) == true)
            {
                txtDocNum.Properties.ReadOnly = true;
                txtDocNum.Text = "##########";

            }
            oSeries.Close();
        }

        string GetNewSeries()
        {
            string _SERIES = "";
            string _OBJECT_TYPE = "17";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_OBJECT_TYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_OBJECT_TYPE);
            }
            else
            {
                _SERIES = txtDocNum.Text;
            }
            oSeries.Close();

            return _SERIES;
        }

        void DisableControls()
        {
            btnAdd.Enabled = false;
            radioSelection.Enabled = false;
            txtReference.Enabled = false;
            txtDocNum.Enabled = false;

            txtRemarks.Enabled = false;
            txtStatus.Enabled = false;
            dtDateCreated.Enabled = false;
            txtCreatedBy.Enabled = false;
            dtDateModified.Enabled = false;
            txtModifiedBy.Enabled = false;
            cboDocType.Enabled = false;
            txtReceiptNumber.Enabled = false;
            dtPostingDate.Enabled = false;
            txtTINNumber.Enabled = false;
            txtCustCode.Enabled = false;
            txtCustName.Enabled = false;
            txtAddress.Enabled = false;


            cboBankCodes.Enabled = false;
            txtCheckNumber.Enabled = false;
            txtCheckAmount.Enabled = false;
            dtDueDate.Enabled = false;
            txtCheckStatus.Enabled = false;


        }

        void EnableControls()
        {
            btnAdd.Enabled = true;
            txtDocNum.Enabled = true;
            radioSelection.Enabled = true;
            txtReference.Enabled = true;
            txtRemarks.Enabled = true;
            txtStatus.Enabled = true;
            dtDateCreated.Enabled = true;
            txtCreatedBy.Enabled = true;
            dtDateModified.Enabled = true;
            txtModifiedBy.Enabled = true;
            cboDocType.Enabled = true;
            txtReceiptNumber.Enabled = true;
            dtPostingDate.Enabled = true;
            txtTINNumber.Enabled = true;
            txtCustCode.Enabled = true;
            txtCustName.Enabled = true;
            txtAddress.Enabled = true;


            cboBankCodes.Enabled = true;
            txtCheckNumber.Enabled = true;
            txtCheckAmount.Enabled = true;
            dtDueDate.Enabled = true;
            txtCheckStatus.Enabled = true;

        }

        private void Receipt_Check_Load(object sender, EventArgs e)
        {
            BindBanks();
            cboDocType.SelectedIndex = 0;
            InitializeSeries();

            txtCreatedBy.Text = this.ActiveUserID;
            txtModifiedBy.Text = this.ActiveUserID;

            dtDateCreated.EditValue = System.DateTime.Now;
            dtDateModified.EditValue = System.DateTime.Now;
            dtPostingDate.EditValue = System.DateTime.Now;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text == "Add")
            {
                CheckReceiptManager oManager = new CheckReceiptManager();
                CheckReceiptUnit oUnit = new CheckReceiptUnit();

                txtDocNum.Text = GetNewSeries();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.Reference = txtReference.Text;
                oUnit.Remarks = txtRemarks.Text;
                oUnit.DocumentType = cboDocType.Text;
                oUnit.ReceiptNumber = txtReceiptNumber.Text;

                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.TINNumber = txtTINNumber.Text;

                if (radioSelection.SelectedIndex == 0)
                {
                    oUnit.IsAccount = false;
                }
                else
                {
                    oUnit.IsAccount = true;
                }

                oUnit.CardCode = txtCustCode.Text;
                oUnit.CardName = txtCustName.Text;
                oUnit.Address = txtAddress.Text;

                oUnit.BankName = cboBankCodes.EditValue.ToString();
                oUnit.CheckNumber = txtCheckNumber.Text;
                oUnit.CheckDate = (DateTime)dtDueDate.EditValue;
                oUnit.CheckAmount = Convert.ToDouble(txtCheckAmount.Text);
                oUnit.CheckStatus = txtCheckStatus.Text;

                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.AddNewCheckEntry(oUnit);

                oManager.Close();

                btnAdd.Text = "Update";


            }
            else
            {
                CheckReceiptManager oManager = new CheckReceiptManager();
                CheckReceiptUnit oUnit = new CheckReceiptUnit();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.Reference = txtReference.Text;
                oUnit.Remarks = txtRemarks.Text;
                oUnit.DocumentType = cboDocType.Text;
                oUnit.ReceiptNumber = txtReceiptNumber.Text;

                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.TINNumber = txtTINNumber.Text;

                if (radioSelection.SelectedIndex == 0)
                {
                    oUnit.IsAccount = false;
                }
                else
                {
                    oUnit.IsAccount = true;
                }

                oUnit.CardCode = txtCustCode.Text;
                oUnit.CardName = txtCustName.Text;
                oUnit.Address = txtAddress.Text;


                oUnit.BankName = cboBankCodes.EditValue.ToString();
                oUnit.CheckNumber = txtCheckNumber.Text;
                oUnit.CheckDate = (DateTime)dtDueDate.EditValue;
                oUnit.CheckAmount = Convert.ToDouble(txtCheckAmount.Text);
                oUnit.CheckStatus = txtCheckStatus.Text;

                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.UpdateCheckEntry(oUnit);

                oManager.Close();
            }
        }

        private void radioSelection_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioSelection.SelectedIndex == 0)
            {
                lblSelection.Text = "Customer";
                lblAddress.Text = "Address:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";


            }
            else
            {
                lblSelection.Text = "Account";
                lblAddress.Text = "Remarks:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";
            }
        }

        private void txtStatus_EditValueChanged(object sender, EventArgs e)
        {
            if (txtStatus.Text == "Posted")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
            }

            if (txtStatus.Text == "Draft")
            {
                EnableControls();
                txtStatus.Properties.Buttons[0].Visible = true;
                txtStatus.Properties.Buttons[1].Visible = true;
            }

            if (txtStatus.Text == "Canceled")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
            }

            if (txtStatus.Text == "Closed")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
            }
        }

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            switch (e.Button.Index)
            {
                case 0:
                    // Post

                    PostDocument(txtDocNum.Text);

                    break;
                case 1:
                    // Cancel:
                    if (txtStatus.Text == "Draft")
                    {
                        CancelDocument(txtDocNum.Text);
                    }
                    else if (txtStatus.Text == "Posted")
                    {
                        CloseDocument(txtDocNum.Text);
                    }
                    break;
            }
        }

        private void txtCustCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            if (radioSelection.SelectedIndex == 0)
            {
                FND_BP _SearchBP = new FND_BP();

                AddressBookManager oBPManager = new AddressBookManager();
                TaxManager oTxManager = new TaxManager();

                try
                {


                    _SearchBP.ConnectionString = this.ConnectionString;
                    _SearchBP.BusinessPartnerType = "Customer";
                    _SearchBP.ShowDialog();

                    txtCustCode.Text = _SearchBP.CardCode;
                    txtCustName.Text = _SearchBP.CardName;
                    txtTINNumber.Text = _SearchBP.TIN;
                    txtAddress.Text = _SearchBP.Address;
                    txtAddress.Enabled = false;
                }
                catch
                {
                }
            }
            else
            {
                FND_GLACCOUNT oForm = new FND_GLACCOUNT();

                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();

                txtTINNumber.Text = "N/A";
                txtAddress.Enabled = true;
                txtCustCode.Text = oForm.SelectedGLAcctCode;
                txtCustName.Text = oForm.SelectedGLAcctName;

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
